<?php
namespace lib;

require_once('config.php');

class Redis
{
    private static $instance = null;
    private $redis;

    private function __construct()
    {
        $this->redis = new Redis();
        $result = $this->redis->connect(self::REDIS_HOST, self::REDIS_PORT, self::REDIS_TIMEOUT);
        if ($result === false) {
            throw new \Exception('Redis connect fail');
        }
    }

    public static function instance()
    {
        if (self::$instance == null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function __call($name, $arguments)
    {
        $count = count($arguments);
        switch ($count) {
            case 1:
                return $this->redis->$name($arguments[0]);
            case 2:
                return $this->redis->$name($arguments[0], $arguments[1]);
            case 3:
                return $this->redis->$name($arguments[0], $arguments[1], $arguments[2]);
            case 4:
                return $this->redis->$name($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
            default:
                throw new Exception('方法参数错误');
        }
    }

}